﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Crosland.Sami.Logic;
using Crosland.Automotriz.Logic.Linq;
using Crosland.Automotriz.Core;
using System.Text;
using System.Drawing;
using System.Configuration;
using Crosland.Automotriz.Logic.ControlExeption;
namespace Crosland.Automotriz.Web.Repuestos
{
    public partial class wFrmRelacionarArticulo : BaseForm
    {
        string idNegocio = ConfigurationManager.AppSettings["Negocio"];
        clsTD_articulo_servicioLogic NegocioArticulo = new clsTD_articulo_servicioLogic();


        clsLog_Sus_ArtLogic artsusBL = new clsLog_Sus_ArtLogic();
        T_LOG_ARTICULO_RELACIONADO art_relacion = new T_LOG_ARTICULO_RELACIONADO();

        protected override void Inicializar()
        {

            Panel img = (Panel)Master.FindControl("asp");
            img.Visible = false;
            this.ListadoArticulosDetalle();



        }



        void ListadoArticulosDetalle()
        {

            try
            {
                dgDetalleMaeSus.DataSource = NegocioArticulo.ListarArticuloV2();
                dgDetalleMaeSus.DataBind();

            }
            catch (DataException ex)
            {
                GuardarExceptionLog(ex.Message, ex, "ListadoArticulosDetalle");
                MensajesError(ex.Message, ex, "ListadoArticulosDetalle");
            }
            catch (Exception ex)
            {
                GuardarExceptionLog(ex.Message, ex, "ListadoArticulosDetalle");
                MensajesError(ex.Message, ex, "ListadoArticulosDetalle");
            }
        }

        private void GuardarExceptionLog(String msgUsu, Exception ex, String metError)
        {
            try
            {
                String msgReal, fhError, pagError, codUsu;
                msgReal = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
                pagError = new System.IO.FileInfo(Page.Request.Url.AbsolutePath).Name;
                codUsu = Session["User"].ToString();
                fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");

                System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
                int number = 0;
                for (int i = 0; i < trace.FrameCount; i++)
                {
                    System.Diagnostics.StackFrame sf = trace.GetFrame(i);
                    number = sf.GetFileLineNumber();
                }
                fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
                new Logg().ErrorLog(Server.MapPath("~/Logs/ErrorLog.csv"), msgUsu, msgReal, pagError, metError, codUsu, number);
            }
            catch (DataException e)
            {

                MensajesError(e.Message, e, "GuardarExceptionLog");
            }
            catch (RunException re)
            {
                MensajesError(re.Message, re, "GuardarExceptionLog");
            }

        }

        private void MensajesError(String msgUsu, Exception ex, String metError)
        {
            Crosland.Automotriz.Web.UserControl.MensajeError uc = (Crosland.Automotriz.Web.UserControl.MensajeError)Master.FindControl("ucErrorMensaje");
            String msgReal, fhError, pagError, codUsu;
            msgReal = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
            pagError = new System.IO.FileInfo(Page.Request.Url.AbsolutePath).Name;
            codUsu = Session["User"].ToString();
            fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
            System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
            int number = 0;
            for (int i = 0; i < trace.FrameCount; i++)
            {
                System.Diagnostics.StackFrame sf = trace.GetFrame(i);
                number = sf.GetFileLineNumber();
            }
            uc.Errores(msgUsu, msgReal, fhError, pagError, metError, codUsu, number);
        }





        protected void cmdSalir_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Seguridad/wFrmDefault.aspx?id=" + idNegocio);
        }

        protected void CmdNuevo_Click(object sender, EventArgs e)
        {
            Session["operacion"] = "N";
            Response.Redirect("~/Repuestos/wFrmRelacionarArticuloActualizar.aspx?id=" + idNegocio);
        }



        protected void cmdModificar_Click(object sender, EventArgs e)
        {
            //try
            //{
            //Session["ArtMae"] = (string)dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "Cod_Articulo_Serv_Princ");
            //Session["ArtSus"] = (string)dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "Cod_articulo_serv_Rel");
            //Session["CodFamilia"] = (string)dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "Cod_Familia_Princ");
            //Session["CodFamiliaSus"] = (string)dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "cod_familia_Rel");
            //Session["CodEmpresa"] = "0008";

            //}
            //catch 
            //{}


        }

        protected void btneliminacion_Click(object sender, EventArgs e)
        {

        }

        protected void btnPopAceptar_Click(object sender, EventArgs e)
        {

            try
            {
                string cod_material = Convert.ToString(dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "COD_MATERIAL"));
                string cod_material_sus = Convert.ToString(dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "COD_MATERIAL_SUS"));

                if (artsusBL.PA_EliminarDetalle(cod_material, cod_material_sus))
                {

                    this.ListadoArticulosDetalle();


                }
                else
                {
                }
            }
            catch
            { }

        }

        protected void dgDetalleMaeSus_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            this.ListadoArticulosDetalle();
        }

        protected void dgDetalleMaeSus_PageIndexChanged(object sender, EventArgs e)
        {
            this.ListadoArticulosDetalle();
        }

        protected void btnModificarArt_Click(object sender, EventArgs e)
        {
            try
            {
                Session["operacion"] = "M";
                Session["COD_MATERIAL"] = Convert.ToString(dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "COD_MATERIAL"));
                Session["DESCRIPCION_MAE"] = Convert.ToString(dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "DESCRIPCION_MAE"));
                Session["ID_MATERIAL"] = Convert.ToInt32(dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "ID_MATERIAL"));
                Session["ID_SUS_ART_MAE"] = Convert.ToString(dgDetalleMaeSus.GetRowValues(dgDetalleMaeSus.FocusedRowIndex, "ID_SUS_ART_MAE"));
                Response.Redirect("~/Repuestos/wFrmRelacionarArticuloActualizar.aspx?id=" + idNegocio);

            }
            catch
            { }
        }





    }
}